<?php defined('SUROBOT') or exit('No direct script access allowed');

/**
 * 静态工具类
 * @author WispX
 *
 */
class Fun
{
	
	/**
	 * Curl
	 * @param string $url
	 * @return unknown
	 */
	public static function Curl($url, $data = "")
	{
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_HEADER, 0);
		if($data != "") curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
		$result = curl_exec($curl);
		curl_close($curl);
		return $result;
	}
	
	/**
	 * 根据URL返回html a链接标签
	 * @param string $url
	 * @return string
	 */
	public static function GetUrlHtmlA($url)
	{
		return "<a href=\"{$url}\" target=\"_block\">". urldecode($url) ."</a>";
	}
	
	/**
	 * 判断两个值是否相同
	 * @param string $str1
	 * @param string $str2
	 * @param boolean $type 是否判断类型
	 * @return boolean
	 */
	public static function Equals($str1, $str2, $type = false)
	{
		if(!$type) {
			return $str1 == $str2 ? true : false;
		}
		return $str1 === $str2 ? true : false;
	}
	
	/**
	 * 接收get || post参数
	 * @param unknown $str
	 * @return NULL|unknown
	 */
	public static function GetParam($str)
	{
		$get = !empty($_GET[$str]) ? $_GET[$str] : null;
		if($get == null || $get == "") {
			$post = !empty($_POST[$str]) ? $_POST[$str] : null;
		}
		return $get != null ? $get : $post;
	}
	
	/**
	 * 获取客户端IP
	 * @return unknown|boolean
	 */
	public static function GetIp()
	{
		$ip = false;
		if(!empty($_SERVER["HTTP_CLIENT_IP"])){
			$ip = $_SERVER["HTTP_CLIENT_IP"];
		}
		if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
			$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
			if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
			for ($i = 0; $i < count($ips); $i++) {
				if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
					$ip = $ips[$i];
					break;
				}
			}
		}
		return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
	}
}

?>